<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Figure 8.11: Approximate linear discrimination via support vector classifier</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch08_geometric_probs/html/svm_2.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Figure 8.11: Approximate linear discrimination via support vector classifier</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 8.6.1, Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Original by Lieven Vandenberghe</span>
<span class="comment">% Adapted for CVX by Joelle Skaf - 10/16/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% The goal is to find a function f(x) = a'*x - b that classifies the non-</span>
<span class="comment">% separable points {x_1,...,x_N} and {y_1,...,y_M} by doing a trade-off</span>
<span class="comment">% between the number of misclassifications and the width of the separating</span>
<span class="comment">% slab. a and b can be obtained by solving the following problem:</span>
<span class="comment">%           minimize    ||a||_2 + gamma*(1'*u + 1'*v)</span>
<span class="comment">%               s.t.    a'*x_i - b &gt;= 1 - u_i        for i = 1,...,N</span>
<span class="comment">%                       a'*y_i - b &lt;= -(1 - v_i)     for i = 1,...,M</span>
<span class="comment">%                       u &gt;= 0 and v &gt;= 0</span>
<span class="comment">% where gamma gives the relative weight of the number of misclassified</span>
<span class="comment">% points compared to the width of the slab.</span>

<span class="comment">% data generation</span>
n = 2;
randn(<span class="string">'state'</span>,2);
N = 50; M = 50;
Y = [1.5+0.9*randn(1,0.6*N), 1.5+0.7*randn(1,0.4*N);
     2*(randn(1,0.6*N)+1), 2*(randn(1,0.4*N)-1)];
X = [-1.5+0.9*randn(1,0.6*M),  -1.5+0.7*randn(1,0.4*M);
      2*(randn(1,0.6*M)-1), 2*(randn(1,0.4*M)+1)];
T = [-1 1; 1 1];
Y = T*Y;  X = T*X;
g = 0.1;            <span class="comment">% gamma</span>

<span class="comment">% Solution via CVX</span>
cvx_begin
    variables <span class="string">a(n)</span> <span class="string">b(1)</span> <span class="string">u(N)</span> <span class="string">v(M)</span>
    minimize (norm(a) + g*(ones(1,N)*u + ones(1,M)*v))
    X'*a - b &gt;= 1 - u;
    Y'*a - b &lt;= -(1 - v);
    u &gt;= 0;
    v &gt;= 0;
cvx_end

<span class="comment">% Displaying results</span>
linewidth = 0.5;  <span class="comment">% for the squares and circles</span>
t_min = min([X(1,:),Y(1,:)]);
t_max = max([X(1,:),Y(1,:)]);
tt = linspace(t_min-1,t_max+1,100);
p = -a(1)*tt/a(2) + b/a(2);
p1 = -a(1)*tt/a(2) + (b+1)/a(2);
p2 = -a(1)*tt/a(2) + (b-1)/a(2);

graph = plot(X(1,:),X(2,:), <span class="string">'o'</span>, Y(1,:), Y(2,:), <span class="string">'o'</span>);
set(graph(1),<span class="string">'LineWidth'</span>,linewidth);
set(graph(2),<span class="string">'LineWidth'</span>,linewidth);
set(graph(2),<span class="string">'MarkerFaceColor'</span>,[0 0.5 0]);
hold <span class="string">on</span>;
plot(tt,p, <span class="string">'-r'</span>, tt,p1, <span class="string">'--r'</span>, tt,p2, <span class="string">'--r'</span>);
axis <span class="string">equal</span>
title(<span class="string">'Approximate linear discrimination via support vector classifier'</span>);
<span class="comment">% print -deps svc-discr2.eps</span>
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Calling sedumi: 204 variables, 100 equality constraints
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
Split 1 free variables
eqs m = 100, order n = 205, dim = 206, blocks = 2
nnz(A) = 200 + 400, nnz(ADA) = 100, nnz(L) = 100
Handling 5 + 1 dense columns.
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.09E+00 0.000
  1 :   4.57E+00 5.25E-01 0.000 0.4840 0.9000 0.9000   2.18  1  1  1.1E+00
  2 :   4.02E+00 3.30E-01 0.000 0.6289 0.9000 0.9000   2.94  1  1  4.2E-01
  3 :   2.94E+00 1.01E-01 0.000 0.3061 0.9000 0.9000   2.18  1  1  8.9E-02
  4 :   2.45E+00 4.48E-02 0.000 0.4427 0.9000 0.9000   0.99  1  1  4.8E-02
  5 :   2.20E+00 2.20E-02 0.000 0.4906 0.9000 0.9000   0.78  1  1  2.9E-02
  6 :   2.05E+00 1.19E-02 0.000 0.5417 0.9000 0.9000   0.76  1  1  1.8E-02
  7 :   1.94E+00 5.82E-03 0.000 0.4894 0.9000 0.9000   0.85  1  1  1.0E-02
  8 :   1.88E+00 2.56E-03 0.000 0.4391 0.9000 0.9000   0.91  1  1  4.7E-03
  9 :   1.85E+00 1.09E-03 0.000 0.4273 0.9000 0.9000   0.97  1  1  2.1E-03
 10 :   1.85E+00 9.38E-06 0.000 0.0086 0.9000 0.0000   0.94  1  1  7.2E-04
 11 :   1.83E+00 1.99E-06 0.000 0.2124 0.9146 0.9000   0.99  1  1  1.5E-04
 12 :   1.83E+00 4.10E-08 0.000 0.0206 0.9900 0.9783   0.98  1  1  3.8E-06
 13 :   1.83E+00 1.25E-08 0.000 0.3052 0.7618 0.9000   1.00  1  1  1.2E-06
 14 :   1.83E+00 5.25E-09 0.000 0.4199 0.9000 0.9000   1.00  1  1  4.8E-07
 15 :   1.83E+00 1.07E-09 0.000 0.2033 0.9000 0.9000   1.00  1  1  9.8E-08
 16 :   1.83E+00 9.47E-12 0.000 0.0089 0.9990 0.9990   1.00  3  4  8.1E-10

iter seconds digits       c*x               b*y
 16      0.1   Inf  1.8257002529e+00  1.8257002532e+00
|Ax-b| =   1.5e-09, [Ay-c]_+ =   5.1E-10, |x|=  1.3e+01, |y|=  4.2e-01

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    1.000E-01    0.000E+00    
Max-norms: ||b||=1, ||c|| = 1,
Cholesky |add|=2, |skip| = 0, ||L.L|| = 1.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +1.8257
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="svm_2__01.png" alt=""> 
</div>
</div>
</body>
</html>